package cn.dengqiang.user.service.impl;

import cn.dengqiang.basic.exception.BusinessException;
import cn.dengqiang.basic.service.impl.BaseServiceImpl;
import cn.dengqiang.basic.util.MD5Utils;
import cn.dengqiang.basic.util.StrUtils;
import cn.dengqiang.user.constant.UserConstants;
import cn.dengqiang.user.domain.LoginInfo;
import cn.dengqiang.user.domain.User;
import cn.dengqiang.user.dto.UserDto;
import cn.dengqiang.user.mapper.LoginInfoMapper;
import cn.dengqiang.user.mapper.UserMapper;
import cn.dengqiang.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;

    @Override
    public void phoneRegister(UserDto userDto) {
        //判断非空
        if (StringUtils.isEmpty(userDto.getPhone())
                ||StringUtils.isEmpty(userDto.getPassword())
                ||StringUtils.isEmpty(userDto.getConfirmPwd())
                ||StringUtils.isEmpty(userDto.getVerifyCode()))
            throw new BusinessException("请正确填写信息");
        //1.2 两个密码要一致
        if (!userDto.getPassword().equals(userDto.getConfirmPwd()))
            throw new BusinessException("请正确填写密码");
        //1.3 判断是否已经被注册
        User userTmp = userMapper.loadByPhone(userDto.getPhone());
        if (userTmp!=null)
            throw new BusinessException("用户已被注册");
        //2 验证验证码-吧输入验证码和redis中存的验证码做比对（不要区分大小写）
        Object codeObj = redisTemplate.opsForValue().get(UserConstants.REGISTER_CODE_PREFIX+userDto.getPhone());
        if (codeObj==null)
            throw new BusinessException("验证码已失效，请重新发送");
        String code = ((String)codeObj).split("0")[0];
        if (!userDto.getVerifyCode().equals(code))
            throw new BusinessException("验证码不正确");
        //3 注册
        User user = UserDto2User(userDto);
        LoginInfo info = user2LoginInfo(user);
        //3.2 登录信息表
        loginInfoMapper.save(info);
        user.setInfo(info);
        //3.1 用户表
        userMapper.save(user);
    }

    private LoginInfo user2LoginInfo(User user) {
        LoginInfo info = new LoginInfo();
        BeanUtils.copyProperties(user,info);
        return info;

    }

    private User UserDto2User(UserDto userDto) {
        //1 申明要返回对象
        User user = new User();
        //2 进行转换
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        String securityPwd = MD5Utils.encrypByMd5(userDto.getPassword()+salt);
        user.setSalt(salt);
        user.setPassword(securityPwd);
        //3 返回
        return user;
    }
}
